/**
 * @description 菜单JS
 * @author 赵子靖
 * @since 2017-11-22
 */
var Admin = function() {
    var t = {
        path :'',
        init : function() {
            t.initView();
            t.initEvent();
        },
        initView : function() {
            
        },
        initEvent : function() {
            
        },
        //加载数据
        render:function(){
            $("#admin-table").bootstrapTable({
                queryParams : function(params) {
                    $.extend(params, {
                    })
                    return params;
                }

            });
        },
        //添加
        addAdminDialog:function(parentId,parentName){
            $("#dialog").load(t.path+"/sysAdmin/forwardAddAdmin.do",function(responseTxt,statusTxt,xhr){
                if(statusTxt=="success"){
                    $('#add-admin-dialog').modal();
                    $("#add-admin-dialog #mParentId").val(parentId);
                    $("#add-admin-dialog #mParentName").val(parentName);
                }else if(statusTxt=="error"){
                    layer.alert("添加失败", {
                        closeBtn: 0,
                        shift: 2 //动画类型
                    });
                }
            });
        },
        //保存
        addAdmin:function(){
            var aAccount = $("#add-admin-dialog #aAccount").val();
            var aPwd = $("#add-admin-dialog #aPwd").val();
            var aNicheng = $("#add-admin-dialog #aNicheng").val();
            var aStatus = $("input[name='aStatus']:checked").val();
            var aEmail = $("#add-admin-dialog #aEmail").val();
            var aRemark = $("#add-admin-dialog #aRemark").val();
            var aRoleId = $("#add-admin-dialog #aRoleId").val();
            if(Util.isEmpty(aAccount)){
                $("#aAccountSpan").html("请输入登录账号。");
                $("#add-admin-dialog #aAccount").focus();
                return;
            }
            $("#aAccountSpan").html("");
            if(Util.isEmpty(aPwd)){
                $("#aPwdSpan").html("请输入登录密码。");
                $("#add-admin-dialog #aPwd").focus();
                return;
            }
            $("#aPwdSpan").html("");
            
            
            $.ajaxFileUpload({
                url:t.path+"/sysAdmin/addAdmin.do",
                secureuri:false,
                fileElementId:["aHeadImg"],
                dataType:"json",
                cache:false,
                data:{
                    aAccount:aAccount,
                    aPwd:aPwd,
                    aNicheng:aNicheng,
                    aStatus:aStatus,
                    aEmail:aEmail,
                    aRemark:aRemark,
                    aRoleId:aRoleId
                },
                success:function(data,status){//服务器响应成功处理
                    if(data.success){
                        alert("保存成功!");
                        $("#add-admin-dialog").modal('toggle');
                        $("#admin-table").bootstrapTable('refresh');
                    }else{
                        alert("保存失败!");
                    }
                },
                error:function(data,status,e){//服务器响应失败处理
                    alert(e);   
                }
            });
            
        },
        //编辑节点
        updateAdminDialog:function(id){
            $("#dialog").load(t.path+"/sysAdmin/forwardUpdateAdmin.do?id="+id,function(responseTxt,statusTxt,xhr){
                if(statusTxt=="success"){
                    $('#update-admin-dialog').modal();
                }else if(statusTxt=="error"){
                    layer.alert("编辑失败", {
                        closeBtn: 0,
                        shift: 2 //动画类型
                    });
                }
            });
        },
        //修改
        updateAdmin:function(){
            var aId = $("#update-admin-dialog #aId").val();
            var aAccount = $("#update-admin-dialog #aAccount").val();
            var aPwd = $("#update-admin-dialog #aPwd").val();
            var aNicheng = $("#update-admin-dialog #aNicheng").val();
            var aEmail = $("#update-admin-dialog #aEmail").val();
            var aStatus = $("input[name='aStatus']:checked").val();
            var aRemark = $("#update-admin-dialog #aRemark").val();
            var aRoleId = $("#update-admin-dialog #aRoleId").val();
            if(Util.isEmpty(aAccount)){
                $("#aAccountSpan").html("请输入登录账号。");
                $("#update-admin-dialog #aAccount").focus();
                return;
            }
            $("#aAccountSpan").html("");
            if(Util.isEmpty(aPwd)){
                $("#aPwdSpan").html("请输入登录密码。");
                $("#update-admin-dialog #aPwd").focus();
                return;
            }
            $("#aPwdSpan").html("");
            
            $.ajaxFileUpload({
                url:t.path+"/sysAdmin/updateAdmin.do",
                secureuri:false,
                fileElementId:["aHeadImg"],
                dataType:"json",
                cache:false,
                data:{
                    aId:aId,
                    aAccount:aAccount,
                    aPwd:aPwd,
                    aNicheng:aNicheng,
                    aStatus:aStatus,
                    aEmail:aEmail,
                    aRemark:aRemark,
                    aRoleId:aRoleId
                },
                success:function(data,status){//服务器响应成功处理
                    if(data.success){
                        alert("保存成功!");
                        $("#update-admin-dialog").modal('toggle');
                        $("#admin-table").bootstrapTable('refresh');
                    }else{
                        alert("保存失败!");
                    }
                },
                error:function(data,status,e){//服务器响应失败处理
                    alert(e);   
                }
            });
            
        },
        //删除节点
        deldteAdminDialog:function(id){
            bootbox.confirm({
                size : 'small',
                message : "确认删除该信息，删除后将不可恢复？",
                callback : function(result) {
                    if (result) {
                        $.ajax({
                            type:"POST",
                            url:t.path+"/sysAdmin/deleteAdmin.do",
                            dataType:"json",
                            data:{
                                id:id
                            },
                            async: false,
                            cache:false,
                            success:function(data){
                                if (data && data.success) {
                                    alert("删除成功。");
                                    $("#admin-table").bootstrapTable('refresh');
                                } else {
                                    alert("删除失败.");
                                }
                            }
                        });
                    }
                }
            });
        },
        statusFormatter:function(value,row){
            if(value==1){
                return '正常';
            }else{
                return "<font style='color:red;'>停用</font>";
            }
        },
        fileFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return "<a href='"+t.path+"/upload/"+value+"'>"+value+"</a>";
            }
        },
        dateFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return value.substring(0,10);
            }
        },
        urlFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return "<a href='"+value+"' target='_blank'>"+value+"</a>";
            }
        },
        interfaceNumFormatter:function(value,row){
            return ' <a href="javascript:AdminInterface.loadAdminInterfaceDialog(\''+row.hjlIsId+'\')">'+value+' </a>';
        },
        headImgFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return '<img src="'+t.path+'/upload/'+value+'" class="small-head-img">';
            }else{
                return '<img src="'+t.path+'/img/default-head.png" class="small-head-img">';
            }
        },
        //子菜单
        detailFormatter: function(index, row) {
            var html = "<div style='width:90%; margin:auto;'>";
            html += "<table class='party-special-detail-table' border = '1'  >";
            html += "<tr>";
            html += "<td align = 'center' width = '30%'>菜单名称</td>";
            html += "<td align = 'center' width = '30%' >菜单链接</td>";
            html += "<td align = 'center' width = '20%' >状态</td>";
            html += "<td align = 'center'  width = '20%' >操作</td>";
            html += "</tr>";
            $.ajax({
                type:"POST",
                url:t.path+"/sysAdmin/getAdminByParent.do",
                dataType:"json",
                data:{
                    mParentId:row.mId
                },
                async: false,
                cache:false,
                success:function(data){
                    if(data && data.rows){
                        for (var i = 0; i < data.rows.length; i++) {
                            html += "<tr>";
                            html += "<td align = 'center'>" + data.rows[i].mName + "</td>";
                            html += "<td align = 'center'>" + data.rows[i].mUrl + "</td>";
                            if(data.rows[i].mStatus==1){
                                html += "<td align = 'center'>正常</td>";
                            }else{
                                html += "<td align = 'center'><font style='color:red;'>停用</font></td>";
                            }
                            html += '<td align = \'center\'><a href="javascript:Admin.updateAdminDialog(\'' + data.rows[i].mId + '\')" class="btn btn-default btn-xs">编辑</a>&nbsp; <a href="javascript:Admin.deldteAdminDialog(\'' + data.rows[i].mId + '\')" class="btn btn-default btn-xs">删除</a></td>';
                            html += "</tr>";
                        }
                    }else{
                        html += "<tr><td colspan='4'>暂无数据</td></tr>";
                    }
                }
            });
            
            return html;
        },
        operator : function(value, row) {
            var html = "";
            html += '<a href="javascript:Admin.updateAdminDialog(\'' + value + '\')" class="btn btn-default btn-xs">编辑</a>&nbsp;';
            html += '<a href="javascript:Admin.deldteAdminDialog(\'' + value + '\')" class="btn btn-default btn-xs">删除</a>';
            return html;
        }
    }
    return t;
}();